package com.castle.fortress.admin.orders.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.orders.entity.GoodsOrderEntity;
import com.castle.fortress.admin.orders.dto.GoodsOrderDto;
import com.castle.fortress.admin.orders.service.GoodsOrderService;
import com.castle.fortress.common.enums.OrderInsideStatusEnum;
import com.castle.fortress.common.enums.OrderStatusEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.Date;
import java.util.List;

/**
 * 订单表 控制器
 *
 * @author Mgg
 * @since 2023-01-13
 */
@Api(tags="订单表管理控制器")
@Controller
public class GoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;

    /**
     * 订单表的分页展示
     * @param goodsOrderDto 订单表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单表分页展示")
    @GetMapping("/orders/goodsOrder/page")
    @ResponseBody
//    @RequiresPermissions("orders:goodsOrder:pageList")
    public RespBody<IPage<GoodsOrderDto>> pageGoodsOrder(GoodsOrderDto goodsOrderDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsOrderDto> page = new Page(pageIndex, pageSize);
        IPage<GoodsOrderDto> pages = goodsOrderService.pageGoodsOrderExtends(page, goodsOrderDto);

        return RespBody.data(pages);
    }

    /**
     * 订单表的列表展示
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单表列表展示")
    @GetMapping("/orders/goodsOrder/list")
    @ResponseBody
    public RespBody<List<GoodsOrderDto>> listGoodsOrder(GoodsOrderDto goodsOrderDto){
        List<GoodsOrderDto> list = goodsOrderService.listGoodsOrder(goodsOrderDto);
        return RespBody.data(list);
    }

    /**
     * 订单表保存
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("订单表保存")
    @PostMapping("/orders/goodsOrder/save")
    @ResponseBody
    @RequiresPermissions("orders:goodsOrder:save")
    public RespBody<String> saveGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        if(goodsOrderService.save(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表编辑
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("订单表编辑")
    @PostMapping("/orders/goodsOrder/edit")
    @ResponseBody
    @RequiresPermissions("orders:goodsOrder:edit")
    public RespBody<String> updateGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null || goodsOrderDto.getId() == null || goodsOrderDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        if(goodsOrderService.updateById(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("订单表删除")
    @PostMapping("/orders/goodsOrder/delete")
    @ResponseBody
    @RequiresPermissions("orders:goodsOrder:delete")
    public RespBody<String> deleteGoodsOrder(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsOrderService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 订单表批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("订单表批量删除")
    @PostMapping("/orders/goodsOrder/deleteBatch")
    @ResponseBody
    @RequiresPermissions("orders:goodsOrder:deleteBatch")
    public RespBody<String> deleteGoodsOrderBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsOrderService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表详情
     * @param id 订单表id
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单表详情")
    @GetMapping("/orders/goodsOrder/info")
    @ResponseBody
//    @RequiresPermissions("orders:goodsOrder:info")
    public RespBody<GoodsOrderDto> infoGoodsOrder(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderDto goodsOrderDto =  goodsOrderService.getByIdExtends(id);

        return RespBody.data(goodsOrderDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/orders/goodsOrder/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<GoodsOrderDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<GoodsOrderDto> list = goodsOrderService.listGoodsOrder(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}



    /**
     * 订单发货
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("订单发货")
    @PostMapping("/orders/goodsOrder/delivery")
    @ResponseBody
    public RespBody<String> deliveryGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null || goodsOrderDto.getId() == null || goodsOrderDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        //发货时间设置为当前
        goodsOrderEntity.setDvyTime(new Date());
        //状态改为待收货
        goodsOrderEntity.setOrderStatus(OrderStatusEnum.WAIT_RECEIPT.getCode());
        goodsOrderEntity.setOrderInsideStatus(OrderInsideStatusEnum.WAIT_RECEIPT.getCode());
        if(goodsOrderService.updateById(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 取消订单发货
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("取消订单发货")
    @PostMapping("/orders/goodsOrder/cancelDelivery")
    @ResponseBody
    public RespBody<String> cancelDeliveryGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null || goodsOrderDto.getId() == null || goodsOrderDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        //发货时间设置为当前
        goodsOrderEntity.setDvyTime(null);
        //状态改回为待发货
        goodsOrderEntity.setOrderStatus(OrderStatusEnum.WAIT_DELIVERY.getCode());
        goodsOrderEntity.setOrderInsideStatus(OrderInsideStatusEnum.WAIT_DELIVERY.getCode());
        if(goodsOrderService.updateById(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }



    /**
     * 订单确认收货
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("订单确认收货")
    @PostMapping("/orders/goodsOrder/receipt")
    @ResponseBody
    public RespBody<String> receiptGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null || goodsOrderDto.getId() == null || goodsOrderDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        //状态改为待评价
        goodsOrderEntity.setOrderStatus(OrderStatusEnum.WAIT_EVALUATION.getCode());
        goodsOrderEntity.setOrderInsideStatus(OrderInsideStatusEnum.WAIT_EVALUATION.getCode());
        if(goodsOrderService.updateById(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

}
